import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/home/Home'

//菜单路由
import User from '../components/common/user/User'
import Roles from '../components/common/roles/Roles'
import Rights from '../components/common/rights/Rights'
import Goods from '../components/common/goods/Goods'

//首页home重定向到欢迎页面
import Welcom from '../components/common/Welcom'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "login"
  },
  {
    path: "/login",
    name: "login",
    component: Login
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    redirect:"/welcom",
    children:[
      {path:"/welcom",component:Welcom},
      {path:"/users",component:User},
      {path:"/roles",component:Roles},
      {path:"/rights",component:Rights},
      {path:"/goods",component:Goods}
    ]
  }
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径访问而来
  //next 是一个函数，表示放行
  if (to.path == '/login') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
    if(!tokenStr) return next('/login')
    next();
})

export default router
